﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace MasApp.DAL
{
    public class Search
    {
        public Search()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public static DataTable ExecuteTable(string s)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = s;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable i = new DataTable();
            try
            {
                da.Fill(i);
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        public static object ExecuteScalar(string s)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = s;
            object i = new object();
            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteScalar();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;

        }
        public static DataTable LevelMissionIDs(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT levelmissionid "
                + "FROM            levelmission "
                + " WHERE        (projectid = " + projectid
                + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        public static int RuleID(int indicatorid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT ruleid "
                + " FROM indicator "
                + " WHERE (indicatorid = " + indicatorid
                + ")";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        /// <summary>
        /// 通过projectid 返回 level(leveliid, levelname, judgement, levelvalue)序列，按levelvalue降序
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable Levelvalue2(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT levelid, levelname, judgement, levelvalue "
                + " FROM level, project "
                + " WHERE projectid=" + projectid
                + " AND level.methodid= project.methodid ORDER BY levelvalue DESC";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        public static float LevelValue(int levelmissionid, int indicatorid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT levelvalue "
                + " FROM level, applevel "
                + " WHERE (levelmissionid="+levelmissionid
                + " AND indicatorid = "+indicatorid
                + " AND level.levelid = applevel.levelid"
                + ")";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        /// <summary>
        /// 通过项目ID返回权重类型为“直接赋权”的项目相关的直接权重任务的元素及权值列表
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable WeightValue(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT        weight.elementid, weight.elementtype, weight.weightvalue "
                + "FROM            project INNER JOIN "
                + "         weightmission ON project.projectid = weightmission.projectid INNER JOIN "
                + "         weight ON weightmission.weightmissionid = weight.weightmissionid "
                + " WHERE        (project.projectid = " + projectid
                + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        /// <summary>
        /// 通过项目ID返回对应项目 评权 任务所有的状态
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable WeightMissionState(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT        state "
                + " FROM            weightmission "
                + " WHERE        (projectid = " + projectid + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        /// <summary>
        /// 通过项目ID返回对应项目 评级 任务所有的状态
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable LevelMissionState(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT        state "
                + " FROM            levelmission "
                + " WHERE        (projectid = " + projectid + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        public static int LevelIDinApplevel(Model.applevel appl)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT levelid "
                + " FROM applevel "
                + " WHERE (levelmissionid = " + appl.Levelmissionid
                + " AND indicatorid = " + appl.Indicatorid
                + ")";
            int levelid = 0;
            try
            {
                cmd.Connection.Open();
                levelid = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return levelid;
        }
        public static int HaveAppLevelID(Model.applevel appl)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT applevelid "
                + " FROM applevel "
                + " WHERE (levelmissionid = " + appl.Levelmissionid
                + " AND indicatorid = " + appl.Indicatorid 
                + ")";
            int applevelid = 0;
            try
            {
                cmd.Connection.Open();
                applevelid = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return applevelid;
        }
        public static DataTable Level2(int levelmissionid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT level.levelid, level.levelname, level.judgement, level.methodid, level.levelvalue "
                + " FROM            levelmission INNER JOIN "
                + "         project ON levelmission.projectid = project.projectid INNER JOIN "
                + "         level ON project.methodid = level.methodid "
                + " WHERE        (levelmission.levelmissionid = " + levelmissionid + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        public static DataTable Level(int methodid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT        levelid, levelname, judgement, levelvalue "
                + " FROM            [level] "
                + " WHERE        (methodid = " + methodid + ")";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }
        public static int HaveUserInLevelMission(Model.levelmission lm)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT COUNT(*) "
                + " FROM levelmission "
                + " WHERE (projectid = " + lm.Projectid
                + " AND actor = '" + lm.Actor + "')";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        public static string SearchWeightMissionState(int weightmissionid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT state "
                + " FROM weightmission "
                + " WHERE (weightmissionid = '"
                + weightmissionid
                + "')";
            string state="";
            try
            {
                cmd.Connection.Open();
                state = cmd.ExecuteScalar().ToString();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return state;
        }
        public static int LevelMissionActorRoleID(Model.levelmission lm)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT actorroleid "
                + " FROM levelmission "
                + " WHERE (projectid = "
                + lm.Projectid
                + " AND actor = '"
                + lm.Actor
                + "')";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        public static string SearchLevelMissionState(int levelmissionid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT state "
                + " FROM levelmission "
                + " WHERE (levelmissionid = "
                + levelmissionid
                + ")";
            string state="";
            try
            {
                cmd.Connection.Open();
                state = cmd.ExecuteScalar().ToString();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return state;
        }
        public static int SearchHaveUsernameInWeightmission(string username, int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT COUNT(*) "
                + " FROM weightmission "
                + " WHERE (projectid = "
                + projectid
                + "AND actor = '"
                + username
                + "')";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        public static int SearchHaveUsernameInLevelmission(string username, int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT COUNT(*) "
                + " FROM levelmission "
                + " WHERE (projectid = "
                + projectid
                + "AND actor = '"
                + username
                + "')";
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return i;
        }
        /// <summary>
        /// 通过projectid查找准则ID和名称
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static DataTable SearchRuleIDandName(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT [rule].ruleid, [rule].rulename "
                + " FROM [rule] INNER JOIN model ON [rule].modelid = model.modelid INNER JOIN project ON model.modelid = project.modelid "
                + " WHERE (project.projectid = " + projectid + ")"
                + " ORDER BY [rule].ruleid";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
            }
            return dt;
        }

        /// <summary>
        /// 查找项目赋权方式，返回“直接赋权”或者“序关系法”
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static string SearchProjectWeightType(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT weighttype FROM project WHERE projectid = '" + projectid + "'";
            cmd.CommandText = str;
            string weighttype = "";
            try
            {
                cmd.Connection.Open();
                weighttype = cmd.ExecuteScalar().ToString();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return weighttype;
        }
        /// <summary>
        /// 查找是否存在用户名，返回状态
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool HaveUsername(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT count(*) FROM login WHERE username = '" + username + "'";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return (i > 0) ? true : false;
        }

        public static int SearchUserID(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT userid FROM users WHERE username = '" + username + "'";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = (int)cmd.ExecuteScalar();
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return i;
        }

        public static string SearchUserRealName(string username)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT realname FROM users WHERE username = '" + username + "'";
            cmd.CommandText = str;
            string realname = "";
            try
            {
                cmd.Connection.Open();
                realname = Convert.ToString(cmd.ExecuteScalar());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return realname;
        }

        public static DataSet SearchManaUsers()
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT users.username AS us FROM userrole INNER JOIN users ON userrole.userid = users.userid"
                + "WHERE (userrole.roleid = 1) OR (userrole.roleid = 2)";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                cmd.Connection.Open();
                da.Fill(ds);
                cmd.Connection.Close();
                return ds;
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
                return null;
            }
        }

        public static DataSet SearchOrg()
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT organizationid, creator, organizationname, description FROM organization";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                cmd.Connection.Open();
                da.Fill(ds);
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return ds;
        }
        public static int SearchOrgidByCreator(string username)
        {
            int id = 0;
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "select organizationid from organization where creator = '" + username + "'";
            cmd.CommandText = str;
            try
            {
                cmd.Connection.Open();
                id = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return id;
        }
        public static int SearchOrgID(string username)
        {
            int id = 0;
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "select organizationid from users where username = '" + username + "'";
            cmd.CommandText = str;
            try
            {
                cmd.Connection.Open();
                id = int.Parse(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return id;
        }

        public static DataSet SearchRoleIDandName(int userid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SearchRoleIDandName";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = userid;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            try
            {
                cmd.Connection.Open();
                da.Fill(ds);
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
                System.Web.HttpContext.Current.Response.Write(ee.ToString());
                System.Web.HttpContext.Current.Response.End();
            }
            return ds;
        }
        /// <summary>
        /// 通过projectid查找准则个数
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchRuleNumbers(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT COUNT([rule].ruleid) AS numbers "
                + " FROM model INNER JOIN"
                + " project ON model.modelid = project.modelid INNER JOIN"
                + " [rule] ON model.modelid = [rule].modelid"
                + " WHERE (project.projectid = " + projectid + ")";
            int numbers = 0;
            try
            {
                cmd.Connection.Open();
                numbers = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            {
            }
            return numbers;
        }
       /// <summary>
       /// 通过modelid查找准则个数
       /// </summary>
       /// <param name="modelid"></param>
       /// <returns></returns>
        public static int SearchRuleNumber(int modelid)
        {
            int num = 0;
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT count(ruleid) FROM rule WHERE modelid = " + modelid;
            try
            {
                cmd.CommandText = str;
                cmd.Connection.Open();
                object obj = cmd.ExecuteScalar();

                if (obj == null)
                    num = 0;
                else
                    num = Convert.ToInt32(obj);
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return num;
        }

        public static int SearchIndicatorNumber(int ruleid)
        {
            int num = 0;
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT count(indicatorid) FROM indicator WHERE ruleid = " + ruleid;
            try
            {
                cmd.CommandText = str;
                cmd.Connection.Open();
                object obj = cmd.ExecuteScalar();

                if (obj == null)
                    num = 0;
                else
                    num = Convert.ToInt32(obj);
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return num;
        }
        public static DataSet SearchRuleID(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SearchRuleID";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@modelid", SqlDbType.Int).Value = modelid;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            try
            {
                cmd.Connection.Open();
                da.Fill(ds);
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return ds;
        }

        public static DataTable SearchRuleIDsandNames(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT ruleid, rulename"
                + " FROM [rule]"
                + " WHERE (modelid = " + modelid + ")";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable SearchIndicatorIDsandNames(int ruleid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT indicatorid, indicatorname"
                + " FROM indicator"
                + " WHERE (ruleid = " + ruleid + ")";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }


        public static DataTable SearchAvailModelIDandName(int orgid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT modelid, modelname"
                + " FROM model"
                + " WHERE (organizationid = " + orgid + ") AND available = 'true'";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable SearchModelIDandName(int orgid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT modelid, modelname"
                + " FROM model"
                + " WHERE (organizationid = " + orgid + ")";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public static DataTable SearchAvailMethodIDandName(int orgid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT     methodid, methodname"
                + " FROM         method"
                + " WHERE     (organizationid = " + orgid + ") AND available = 'true'";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return dt;
        }

        public static DataTable SearchMethodIDandName(int orgid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT     methodid, methodname"
                + " FROM         method"
                + " WHERE     (organizationid = " + orgid + ")";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return dt;
        }

        public static int SearchModelID(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT modelid FROM project WHERE projectid = " + projectid;
            cmd.CommandText = str;
            int modelid = 0;
            try
            {
                cmd.Connection.Open();
                modelid = Convert.ToInt32(cmd.ExecuteScalar());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return modelid;
        }

        public static int SearchMethodID(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT methodid FROM project WHERE projectid = " + projectid;
            cmd.CommandText = str;
            int methodid = 0;
            try
            {
                cmd.Connection.Open();
                methodid = Convert.ToInt32(cmd.ExecuteScalar());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return methodid;
        }

        public static MasApp.Model.project SearchOneProject(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT creator, description, endtime,methodid,modelid,"
                + " organizationid, projectname,starttime, createdtime, state, projectid"
                + "  FROM         project"
                + "  WHERE     (projectid =" + projectid + ")";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            MasApp.Model.project pro = new MasApp.Model.project();

            try
            {
                da.Fill(dt);
                pro.Creator = dt.Rows[0][0].ToString();
                pro.Description = dt.Rows[0][1].ToString();
                pro.Methodid = int.Parse(dt.Rows[0][3].ToString());
                pro.Modelid = int.Parse(dt.Rows[0][4].ToString());
                pro.Organizationid = int.Parse(dt.Rows[0][5].ToString());
                pro.Projectname = dt.Rows[0][6].ToString();
                pro.Starttime = Convert.ToDateTime(dt.Rows[0][7].ToString());
                pro.Endtime = Convert.ToDateTime(dt.Rows[0][2].ToString());
                pro.Createdtime = Convert.ToDateTime( dt.Rows[0][8].ToString() );
                pro.State = dt.Rows[0][9].ToString();
                pro.Projectid = int.Parse(dt.Rows[0][10].ToString());
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return pro;
        }

        public static string SearchTarget(int modelid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT target FROM model WHERE modelid = " + modelid;
            cmd.CommandText = str;
            string target = "";
            try
            {
                cmd.Connection.Open();
                target = Convert.ToString(cmd.ExecuteScalar());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return target;
        }
        /// <summary>
        /// 查找权值ID
        /// </summary>
        /// <param name="we">Weightmissionid + we.Elementtype+ we.Elementid</param>
        /// <returns></returns>
        public static int WeightID(Model.weight we)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            cmd.CommandText = "SELECT weightid FROM weight WHERE (weightmissionid = " + we.Weightmissionid
                + ") AND (elementtype='" + we.Elementtype
                + "') AND (elementid = " + we.Elementid + ")";
            int id = 0;
            try
            {
                cmd.Connection.Open();
                id = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return id;
        }
        public static float SearchWeightValue(Model.weight we)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT weightvalue FROM weight WHERE weightmissionid = " + we.Weightmissionid
                + " AND elementtype='" + we.Elementtype
                + "' AND elementid = " + we.Elementid;
            cmd.CommandText = str;
            float weightValue = 0;
            try
            {
                cmd.Connection.Open();
                weightValue = Convert.ToSingle(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return weightValue;
        }
        public static string SearchWeighttype(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT weighttype FROM project WHERE projectid = " + projectid;
            cmd.CommandText = str;
            string weighttype = "";
            try
            {
                cmd.Connection.Open();
                weighttype = Convert.ToString(cmd.ExecuteScalar());
                cmd.Connection.Close();
            }
            catch (Exception ee)
            { }
            return weighttype;
        }
        /// <summary>
        /// 通过organizationid查找用户ID，用户名，真实姓名，通过用户ID降序
        /// </summary>
        /// <param name="orgid"></param>
        /// <returns></returns>
        public static DataTable SearchUsersIDNameName(int orgid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT   userid,username, realname"
                + " FROM         users"
                + " WHERE     (organizationid = " + orgid + ")"
                + " ORDER BY userid DESC";
            cmd.CommandText = str;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return dt;
        }
        /// <summary>
        /// 通过projectid查找weightmissionid
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static int SearchWeightMissionID(int projectid)
        {
            SqlCommand cmd = MasApp.DbUtil.Db.conn();
            string str = "SELECT   weightmissionid "
                + " FROM         weightmission "
                + " WHERE     (projectid = " + projectid + ")";
            cmd.CommandText = str;
            int i = 0;
            try
            {
                cmd.Connection.Open();
                i = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                cmd.Connection.Close();

            }
            catch (Exception ee)
            {
                //System.Web.HttpContext.Current.Response.Redirect("~/showerror.aspx");
            }
            return i;
        }
    }
}